# -*- coding: utf-8 -*-
# created on 2016/6/2


from sympy import fraction
from mathsolver.functions.base import *
from mathsolver.functions.root.jiefangchen import JieFangChen


# 求分式方程的最简公分母
class FenShi001(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        eq = f1 - f2
        new_eq = eq.together().factor()
        numerator, denominator = fraction(new_eq)
        self.steps.append(["方程的最简公分母是", "%s" % new_latex(denominator)])
        self.label.add("求分式方程的最简公分母")
        self.output.append(BasePoly(denominator))
        return self


# 求分式方程的增根
class FenShi004(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        stepsolver_gongfenmu = FenShi001().solver(BaseEq([f1, f2])).output[0].value
        gongfenmu = stepsolver_gongfenmu
        symbol = list(gongfenmu.free_symbols)
        assert (len(symbol) == 1)

        self.steps.append(["分式方程的增根满足", "%s = 0" % new_latex(gongfenmu)])
        jie = JieFangChen().solver(BaseEq([gongfenmu, S.Zero])).output[0].value
        answer = jie[jie.keys()[0]]
        self.steps.append(["解得:", self.output_answer({symbol[0]: answer})])
        self.output.append(BaseSymbolValue({symbol[0]: answer}))
        self.label.add("求分式方程的增根")
        return self


# 分式方程有增根/无解，求参
class FenShi005(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        eq = f1 - f2
        symbol = default_symbol(eq)
        stepsolver1 = FenShi001(verbose=True).solver(args[0])
        self.steps += stepsolver1.steps
        self.label.update(stepsolver1.label)
        gongfenmu = stepsolver1.output[0].value
        stepsolver2 = FenShi004(verbose=True).solver(args[0])
        self.steps += stepsolver2.steps
        self.label.update(stepsolver2.label)
        jies = stepsolver2.output[0].value
        answers = jies[jies.keys()[0]]

        new_f1 = (gongfenmu * f1).cancel()
        new_f2 = (gongfenmu * f2).cancel()
        self.steps.append(["方程去分母后得：", "%s = %s" % (new_latex(new_f1), new_latex(new_f2))])
        for ans in answers:
            sub_f1 = new_f1.subs({symbol: ans})
            sub_f2 = new_f2.subs({symbol: ans})
            if len((sub_f1 - sub_f2).free_symbols) > 0:
                self.steps.append(["将%s = %s带入方程得" % (new_latex(symbol), new_latex(ans)),
                                   "%s = %s" % (new_latex(sub_f1), new_latex(sub_f2))])
                self.output.append(BaseEq([sub_f1, sub_f2]))
                self.label.add("分式方程有增根/无解，求参：类型一")
            else:
                new_eq = (new_f1 - new_f2).factor()
                symbol_poly = []
                if new_eq.is_Mul:
                    for arg in new_eq.args:
                        if arg.has(symbol):
                            symbol_poly.append(arg)
                target_poly = new_eq / symbol_poly[0]
                self.steps.append(
                    ["∵方程的增根为%s,则" % (new_latex(ans)), BaseIneq([target_poly, "!=", S.Zero]).printing()])
                self.output.append(BaseIneq([target_poly, "!=", S.Zero]))
                self.label.add("分式方程有增根，求参：类型二")
        return self


# 分式方程无解问题
class FenShi006(FenShi005):
    pass


# 分式方程去分母
class FenShiQuFenMu(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        stepsolver = FenShi001(verbose=True).solver(BaseEq(eq))
        self.steps += stepsolver.steps
        gongfenmu = stepsolver.output[0].value
        self.steps.append(["", "方程两边同乘以 %s" % (new_latex(gongfenmu))])
        left = (eq[0] * gongfenmu).simplify()
        right = (eq[1] * gongfenmu).simplify()
        self.steps.append(["", BaseEq([left, right]).printing()])
        if right != 0:
            f = (left - right).simplify()
            self.steps.append(["移项，得", BaseEq([f, S.Zero]).printing()])
        return self


if __name__ == '__main__':
    pass
